package org.fyp.synchrona.xml.impl;


import java.io.File;
import java.io.StringReader;
import java.util.LinkedList;
import java.util.List;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.XMLEvent;

import org.fyp.synchrona.util.DataQueue;
import org.fyp.synchrona.util.Task;

import org.codehaus.stax2.XMLInputFactory2;

import com.ctc.wstx.stax.WstxInputFactory;

public class DMMLParser {
	
	private WstxInputFactory xmlInputFactory = null;
	private XMLStreamReader xmlStreamReader = null;
	private XMLStreamWriter xmlStreamWriter = null;
	
	private Task task;
	
	private static DMMLParser dmml;
	
	private DMMLParser(){
		
	}
	
	public static synchronized DMMLParser getDMMLParser(){
		
		if(dmml == null){
			
			dmml = new DMMLParser();
			return dmml;
			
		}else{
			
			return dmml;
			
		}
		
	}
	
	
	public List parseXML(String dmml) throws XMLStreamException{
		xmlInputFactory = new WstxInputFactory();
		
		xmlInputFactory.setProperty(XMLInputFactory2.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
		xmlInputFactory.setProperty(XMLInputFactory2.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
		xmlInputFactory.setProperty(XMLInputFactory2.IS_COALESCING,Boolean.FALSE);
		xmlInputFactory.setProperty(XMLInputFactory2.IS_VALIDATING,Boolean.FALSE);
		xmlInputFactory.configureForSpeed();
		
		List<Task> tasks = new LinkedList<Task>();

		xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(dmml));
		//xmlStreamReader = xmlInputFactory.createXMLStreamReader(f);
		
		int eventType = xmlStreamReader.getEventType();
		String currentElement = "";
		String currentElementText = "";
		
		eventType = xmlStreamReader.next();
		currentElement = xmlStreamReader.getName().toString();
		String xmlType = null;
		if(currentElement.equals("PROFILE")){
			xmlType = "PROFILE";
		}else if(currentElement.equals("STS")){
			xmlType  = "STS";
		}
		eventType = xmlStreamReader.next();
		currentElement = xmlStreamReader.getElementText();
		String SiteURL = currentElement;
		String siteType = null;
		//System.out.println("Printing Site URL :"+SiteURL);
		if(SiteURL.contains("facebook")){
			siteType = "facebook";
		}else if(SiteURL.contains("twitter")){
			siteType = "twitter";
		}else if(SiteURL.contains("Blogger")){
			siteType = "Blogger";
		}else if(SiteURL.contains("moodle")){
			siteType = "Moodle";
		}
		
		while (xmlStreamReader.hasNext()) {
			eventType = xmlStreamReader.next();
			switch(eventType) {
				case XMLEvent.START_ELEMENT:
					currentElement = xmlStreamReader.getName().toString();
					
					if(currentElement.equals("Element")){
						String attributeName = xmlStreamReader.getAttributeValue("","elementName");
						currentElementText = xmlStreamReader.getElementText();
						task = new Task();
						//System.out.println("addin task name the value attributename :"+attributeName);
						task.setAttributeName(attributeName);
						//System.out.println("addin task name the value attributevalue :"+currentElementText);
						task.setAttributeValue(currentElementText);
						//System.out.println("addin task name the value siteurl :"+SiteURL);
						task.setSiteName(SiteURL);
						//System.out.println("addin task name the value type of the site :"+siteType);
						task.setSiteName(siteType);
						//System.out.println("addin task name the value xml type :"+xmlType);
						task.setUpdateType(xmlType);
						
						tasks.add(task);
						//System.out.println(task.getAttributeName());
						//System.out.println(currentElementText);
					}
					
					break;
			}
		}
		return tasks;
	}

}
